surface.CreateFont( "coolvetica", 20, 500, true, false, "ScoreboardPlayerName" )
local texGradient = surface.GetTextureID( "gui/center_gradient" )
local texRatings = {}
texRatings[ 'none' ] 		= surface.GetTextureID( "gui/silkicons/user" )
texRatings[ 'smile' ] 		= surface.GetTextureID( "gui/silkicons/emoticon_smile" )
texRatings[ 'bad' ] 		= surface.GetTextureID( "gui/silkicons/exclamation" )
texRatings[ 'love' ] 		= surface.GetTextureID( "gui/silkicons/heart" )
texRatings[ 'artistic' ] 	= surface.GetTextureID( "gui/silkicons/palette" )
texRatings[ 'star' ] 		= surface.GetTextureID( "gui/silkicons/star" )
texRatings[ 'builder' ] 	= surface.GetTextureID( "gui/silkicons/wrench" )
surface.GetTextureID( "gui/silkicons/emoticon_smile" )
local PANEL = {}

function PANEL:Paint()
	local color = Color(1,1,150,255)
	if ( self.Armed ) then
		color = Color(color.r + 50, color.g + 50, color.b + 50, 255)
	end
	if ( self.Selected ) then
		color = Color(color.r + 50, color.g + 50, color.b + 50, 255)
	end
	if ( self.Player:Team() == TEAM_CONNECTING ) then
		color = Color( 200, 120, 50, 255 )
	end
	if ( self.Open || self.Size != self.TargetSize ) then
		draw.RoundedBox( 4, 0, 16, self:GetWide(), self:GetTall() - 16, color )
		draw.RoundedBox( 4, 2, 16, self:GetWide()-4, self:GetTall() - 16 - 2, Color( 250, 250, 245, 255 ) )
		surface.SetTexture( texGradient )
		surface.SetDrawColor( 255, 255, 255, 255 )
		surface.DrawTexturedRect( 2, 16, self:GetWide()-4, self:GetTall() - 16 - 2 ) 
	end
	draw.RoundedBox( 4, 0, 0, self:GetWide(), 24, color )
	surface.SetTexture( texGradient )
	surface.SetDrawColor( 0, 180, 30, 20 )
	surface.DrawTexturedRect( 0, 0, self:GetWide(), 24 ) 
	surface.SetTexture( self.texRating )
	surface.SetDrawColor( 255, 255, 255, 255 )
	surface.DrawTexturedRect( 4, 4, 16, 16 ) 	
	return true
end

function PANEL:SetPlayer( ply )
	self.Player = ply
	self.infoCard:SetPlayer( ply )
	self:UpdatePlayerData()
end

function PANEL:CheckRating( name, count )
	if ( self.Player:GetNetworkedInt( "Rating."..name, 0 ) > count ) then
		count = self.Player:GetNetworkedInt( "Rating."..name, 0 )
		self.texRating = texRatings[ name ]
	end
	return count
end

function PANEL:UpdatePlayerData()
	if ( !self.Player &&  !self.Player:IsValid() ) then return end
	local pl = self.Player
	local w, h = surface.GetTextSize( pl:GetNWFloat("Money"))
	self.lblMoney:SetPos( self:GetWide() - 50 * 3 -w/2.5, 0 )
	self.lblName:SetText( pl:Nick() )
	self.lblMoney:SetText( pl:GetNWFloat("Money") )
	self.lblPing:SetText( pl:Ping() )
	self.texRating = surface.GetTextureID( "gui/silkicons/emoticon_smile" )
	self.texRating = texRatings[ 'none' ]
	local count = 0

	count = self:CheckRating( 'smile', count )
	count = self:CheckRating( 'love', count )
	count = self:CheckRating( 'artistic', count )
	count = self:CheckRating( 'star', count )
	count = self:CheckRating( 'builder', count )
	count = self:CheckRating( 'bad', count )
end

function PANEL:Init()
	self.Size = 24
	self:OpenInfo( false )
	self.infoCard	= vgui.Create( "ScorePlayerInfoCard", self )
	self.lblName 	= vgui.Create( "Label", self )
	self.lblMoney 	= vgui.Create( "Label", self )
	self.lblPing 	= vgui.Create( "Label", self )
	self.lblName:SetMouseInputEnabled( false )
	self.lblMoney:SetMouseInputEnabled( false )
	self.lblPing:SetMouseInputEnabled( false )
end

function PANEL:ApplySchemeSettings()
	self.lblName:SetFont( "ScoreboardPlayerName" )
	self.lblMoney:SetFont( "ScoreboardPlayerName" )
	self.lblPing:SetFont( "ScoreboardPlayerName" )
	self.lblName:SetFGColor( color_white )
	self.lblMoney:SetFGColor( color_white )
	self.lblPing:SetFGColor( color_white )
end

function PANEL:DoClick( x, y )
	if ( self.Open ) then
		surface.PlaySound( "ui/buttonclickrelease.wav" )
	else
		surface.PlaySound( "ui/buttonclick.wav" )
	end
	self:OpenInfo( !self.Open )
end

function PANEL:OpenInfo( bool )
	if ( bool ) then
		self.TargetSize = 150
	else
		self.TargetSize = 24
	end
	self.Open = bool
end

function PANEL:Think()
	if ( self.Size != self.TargetSize ) then
		self.Size = math.Approach( self.Size, self.TargetSize, (math.abs( self.Size - self.TargetSize ) + 1) * 10 * FrameTime() )
		self:PerformLayout()
		SCOREBOARD:InvalidateLayout()
		self:GetParent():InvalidateLayout()
	end
	if ( !self.PlayerUpdate || self.PlayerUpdate < CurTime() ) then
		self.PlayerUpdate = CurTime() + 0.5
		self:UpdatePlayerData()
	end
end

function PANEL:PerformLayout()
	self:SetSize( self:GetWide(), self.Size )
	self.lblName:SizeToContents()
	self.lblName:SetPos( 24, 2 )
	local COLUMN_SIZE = 50
	self.lblPing:SetPos( self:GetWide() - COLUMN_SIZE * 1, 0 )
	if ( self.Open || self.Size != self.TargetSize ) then
		self.infoCard:SetVisible( true )
		self.infoCard:SetPos( 4, self.lblName:GetTall() + 10 )
		self.infoCard:SetSize( self:GetWide() - 8, self:GetTall() - self.lblName:GetTall() - 10 )
	else
		self.infoCard:SetVisible( false )
	end
end

function PANEL:HigherOrLower( row )
	if ( !self.Player:IsValid() || self.Player:Team() == TEAM_CONNECTING ) then return false end
	if ( !row.Player:IsValid() || row.Player:Team() == TEAM_CONNECTING ) then return true end
	if ( self.Player:GetNWFloat("Money") == row.Player:GetNWFloat("Money") ) then
		return self.Player:Deaths() < row.Player:Deaths()
	end
	return self.Player:GetNWFloat("Money") > row.Player:GetNWFloat("Money")
end

vgui.Register( "ScorePlayerRow", PANEL, "Button" )